﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PDAMain.pdaWebReference;
using PDAMain.Entity;

namespace PDAMain.DAL.Dao
{
    class UserDao : BaseDao
    {
        /// <summary>
        /// 获取已经登入的用户账号
        /// </summary>
        /// <returns></returns>
        public string getLoginedUserAccount()
        {
            string sql = "SELECT ACCOUNT FROM LOGINSTATE";
            object obj = base.ExecuteScalar(sql);
            if (obj != null)
                return obj.ToString();
            else
                return null;
        }

        public string login(string account, string password, string macAddress)
        {
            PdaService pdaService = base.getPdaWebService();
            long result = pdaService.verifyAuth(account, password, macAddress);
            if (result == 4)
            {

                string nowtime = System.DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                string sql = "INSERT INTO LOGINSTATE(ACCOUNT,LOGINDT,STATE) VALUES('" + account + "','" + nowtime + "','" + 1 + "')";
                bool bl = base.executeNonQuery(sql);
                if (bl)
                {
                    Identity.dutyPerson = account;
                    Identity.RoleCode = pdaService.listUserRoles(account);
                    return "";
                }
                else
                {
                    return "保存登入状况是产生错误！";
                }
            }
            else if (result == 3)
            {
                return "设备没有注册,请先注册设备！";
            }
            else if (result == 2)
            {
                return "请输入正确的密码！";
            }
            else
            {
                return "请输入正确的账号！";
            }
        }
        public void logout()
        {
            string sql = "DELETE FROM LOGINSTATE ";
            base.executeNonQuery(sql);
        }
        public string getUserRoles(string userAccount)
        {
            PdaService pdaService = base.getPdaWebService();
            return pdaService.listUserRoles(userAccount);
        }
        public bool modifyPassword(string userAccount,string oldPassword,string newPassword) 
        {
            PdaService pdaService = base.getPdaWebService();
            return pdaService.modifyPassword(userAccount,oldPassword,newPassword);
        }
      
    }
}
